package com.bootdo.wx.service.impl;

import com.alibaba.fastjson.JSON;
import com.bootdo.wx.dao.*;
import com.bootdo.wx.domain.*;
import com.bootdo.wx.dto.FindGoodsDto;
import com.bootdo.wx.dto.GoodsDto;
import com.bootdo.wx.service.GoodsGoodsService;
import com.bootdo.wx.service.GoodsImagesService;
import com.bootdo.wx.service.GoodsProductService;
import com.bootdo.wx.service.GoodsSpikeService;
import com.bootdo.common.config.Constant;
import com.bootdo.common.utils.PageHelperUtils;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.bootdo.common.utils.ShiroUtils.getUserId;


@Service
public class GoodsGoodsServiceImpl implements GoodsGoodsService {
	@Autowired
	private GoodsGoodsDao goodsGoodsDao;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private GoodsProductDao goodsProductDao;
	@Autowired
	private GoodsImagesService goodsImagesService;
	@Autowired
	private GoodsProductService goodsProductService;
	@Autowired
	private GoodsImagesDao goodsImagesDao;
	@Autowired
	private GoodsSpikeService goodsSpikeService;
	@Autowired
	private MemberFollowDao memberFollowDao;
	@Autowired
	private OrderComentDao orderComentDao;
	@Autowired
	private GoodsSpikeDao goodsSpikeDao;
	@Autowired
	private GoodsNineDao goodsNineDao;
	@Autowired
	private GoodsBargainDao goodsBargainDao;
    @Autowired
    private GoodsSpikeTimeDao goodsSpikeTimeDao;
	@Autowired
	private CmsContentDao cmsContentDao;
	@Autowired
	private GoodsTeamDao goodsTeamDao;
	@Autowired
	private GoodsUpgradeDao goodsUpgradeDao;


	/*@Autowired
	private ShopEstempService shopEstempService;*/
	/*@Autowired
    private OrderCommentService orderCommentService;
	@Autowired
    private ShopShippingService shopShippingService;*/
	
	@Override
	public GoodsGoodsDO get(String id){
		return goodsGoodsDao.get(id);
	}
	
	@Override
	public List<GoodsGoodsDO> list(Map<String, Object> map){
		return goodsGoodsDao.list(map);
	}

	@Override
	public List<Map<String, Object>> queryAllForIdAndName(Map<String, Object> map) {
		return goodsGoodsDao.queryAllForIdAndName(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return goodsGoodsDao.count(map);
	}
	
	@Override
	public int save(GoodsGoodsDO goodsGoods){
		return goodsGoodsDao.save(goodsGoods);
	}
	
	@Override
	public int update(GoodsGoodsDO goodsGoods){
		return goodsGoodsDao.update(goodsGoods);
	}
	
	@Override
	public int remove(String id){
		return goodsGoodsDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return goodsGoodsDao.batchRemove(ids);
	}

	@Override
	public R findGoods(Map<String, Object> params) {
		String userId = com.bootdo.common.utils.StringUtils.getString(params.get("userId"));
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		int level = 0;
		if (memberUserDO != null) {
			params.put("level", memberUserDO.getLevel());
			level = memberUserDO.getLevel();
		} else {
			params.put("level", 0);
			level = 0;
		}
		PageHelperUtils.page(params);
		List<Map<String, Object>> goodsList = goodsGoodsDao.findByName(params);
		for (Map<String, Object> goodsMap : goodsList) {
			int isvip = Integer.valueOf(goodsMap.get("isvip").toString());
			String imgurl = goodsMap.get("imgurl").toString();
			List<String> imgList = new ArrayList<String>();
			imgList.add(imgurl);
			goodsMap.remove("imgurl");
			goodsMap.put("imgList",imgList);
			Object price = goodsMap.get("price");
			Object price_c = goodsMap.get("price_c");
			Object price_z = goodsMap.get("price_z");
			Object price_g = goodsMap.get("price_g");
			if (isvip == 0) {
				goodsMap.put("minePrice",price);
			} else {
				switch (level) {
					case 1 :
						goodsMap.put("minePrice",price_c);
						break;
					case 2 :
						goodsMap.put("minePrice",price_z);
						break;
					case 3 :
						goodsMap.put("minePrice",price_g);
						break;
					default:
						goodsMap.put("minePrice",price);
				}
			}
		}
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(goodsList);
		return R.ok().put("data", pageInfo);
	}

    @Override
    public R getHotList(Map<String, Object> map) {
        PageHelperUtils.page(map);
        List<Map<String,Object>> goods = goodsGoodsDao.getHotList(map);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(goods);
        return R.ok().put("data", pageInfo);
    }

	@Transactional(propagation= Propagation.REQUIRED)
	@Override
	public int add(GoodsGoodsDO goodsGoods, String products, String spec_values, String param_values, String images,
			String imagesDetail) {
		
		goodsGoods.setId(UuidUtil.get32UUID());
		goodsGoods.setParam(param_values);
		goodsGoods.setSpec(spec_values);
		goodsGoods.setCategory(0);
//		goodsGoods.setProp(prop_values);
		Date date = new Date();
		if (goodsGoods.getDisabled()==0) {//上架时间
			goodsGoods.setUpAt(date);
		}

		if(goodsGoods.getNumSale()==null){
			goodsGoods.setNumSale(0);
		}
		if(goodsGoods.getIsvip()==null){
			goodsGoods.setIsvip(0);
		}
		//设置参数
		goodsGoods.setGmtCreate(date);
		goodsGoods.setNumComment(0);
		goodsGoods.setNumView(0);
		goodsGoods.setNumSaleWeek(0);
		goodsGoods.setDelFlag(Constant.DEFAULT_DEL_FLAG);
		goodsGoods.setUserIdCreate(getUserId());
		goodsGoods.setRecomSy(0);
		goodsGoods.setRecomXq(0);
		goodsGoods.setRecomGwc(0);
		goodsGoodsDao.save(goodsGoods);

		//商品图片
		List<Map<String,Object>> imgArr = (List<Map<String,Object>>)JSON.parse(images);
		//商品相册
//		HashMap<String,Object>[] imgArr = Json.fromJsonAsArray(NutMap.class, images);
		if (imgArr != null) {
			String defaultImge = "";
			for (int i = 0; i < imgArr.size(); i++) {
				if (i == 0 || (boolean)imgArr.get(i).get("d")) defaultImge = imgArr.get(i).get("url").toString();
				GoodsImagesDO goodsImage = new GoodsImagesDO();
				goodsImage.setGoodsId(goodsGoods.getId());
				goodsImage.setLocation(i);
				goodsImage.setAddress(0);
				goodsImage.setImgurl(imgArr.get(i).get("url").toString());
				goodsImage.setId(UuidUtil.get32UUID());
				goodsImage.setGmtCreate(date);
				goodsImage.setUserIdCreate(getUserId());
				goodsImage.setDelFlag(Constant.DEFAULT_DEL_FLAG);
				goodsImagesService.save(goodsImage);
			}
			goodsGoods.setImgurl(defaultImge);
			goodsGoodsDao.update(goodsGoods);
		}

		//商品货品
		List<Map<String,Object>> productArr = (List<Map<String,Object>>) JSON.parse(products);
//		productArr
//		HashMap[] productArr = Json.fromJsonAsArray(HashMap.class, products);
		if (productArr != null) {
			if (goodsGoods.getHasSpec()==1 && StringUtils.isBlank(productArr.get(0).get("sku").toString())) {
				//如果没有启用规格，并且第一个货品SKU为null或空白字符则自动生成SKU
				productArr.get(0).put("sku", goodsProductService.getSkuPrefix());
			}
			for (int i = 0; i < productArr.size(); i++) {
				String name = "";
				String spec = productArr.get(i).get("spec").toString();
				GoodsProductDO product = new GoodsProductDO();
				product.setId(UuidUtil.get32UUID());
				product.setGoodsId(goodsGoods.getId());
				product.setLocation(i);
				product.setSpec(spec);
				product.setUnit(goodsGoods.getUnit());
				product.setIcon(goodsGoods.getImgurl());
				//如果SKU为null或空白字符则自动生成SKU
				if (StringUtils.isBlank(productArr.get(i).get("sku").toString())) {
					product.setSku(goodsProductService.getSkuPrefix() + "-" + i);
				} else {
					product.setSku(productArr.get(i).get("sku").toString().toUpperCase());
				}
				//通过规格获取名称
				StringBuffer bufName = new StringBuffer();
				if (spec.contains("*") && spec.contains(":")) {
					String[] str = StringUtils.split(spec, "*");
					for (String temp : str) {
						String[] s = StringUtils.split(temp, ":");
//						name += s[1] + " ";
						bufName.append(s[1] + " ");
					}
				} else if (spec.contains(":")) {
					String[] s = StringUtils.split(spec, ":");
//					name = s[1];
					bufName.append(s[1]);
				}
				name=bufName.toString();
				product.setName(name);
				Boolean isDefault = (boolean)productArr.get(i).get("isDefault");
				if (isDefault){
					product.setIsDefault(1);
				}else {
					product.setIsDefault(0);
				}
				Boolean disabled = (boolean)productArr.get(i).get("disabled");
				if (disabled){
					product.setDisabled(1);
				}else {
					product.setDisabled(0);
                    //上架时间
                    product.setUpAt(date);
				}
				String stock = productArr.get(i).get("stock").toString().replace(" ", "");
				product.setStock(NumberUtils.toInt(stock, 0));
				product.setBuyMin(NumberUtils.toInt(productArr.get(i).get("buyMin").toString().replace(" ", ""), 0));
				product.setBuyMax(NumberUtils.toInt(productArr.get(i).get("buyMax").toString().replace(" ", ""), 0));
				product.setWeight(NumberUtils.toInt(productArr.get(i).get("weight").toString().replace(" ", ""), 0));
				product.setPrice(new BigDecimal(productArr.get(i).get("price").toString()));
				product.setPriceC(new BigDecimal(productArr.get(i).get("priceC").toString()));
				product.setPriceZ(new BigDecimal(productArr.get(i).get("priceZ").toString()));
				product.setPriceG(new BigDecimal(productArr.get(i).get("priceG").toString()));
				if(productArr.get(i).get("postFee").toString()==null||"".equals(productArr.get(i).get("postFee").toString())){
					product.setPostFee(new BigDecimal("0.00"));
				}else{
					product.setPostFee(new BigDecimal(productArr.get(i).get("postFee").toString()));
				}
				product.setRebateOne(new BigDecimal(productArr.get(i).get("rebateOne").toString()));
				product.setRebateTwo(new BigDecimal(productArr.get(i).get("rebateTwo").toString()));
				//设置默认参数
				product.setNumSale(0);
				product.setLocation(i);
				product.setUserIdCreate(getUserId());
				product.setGmtCreate(date);
				product.setDelFlag(Constant.DEFAULT_DEL_FLAG);
				goodsProductService.save(product);
				//会员价

			}
		}


		//商品货品
		List<Map<String,Object>> imgDetailArr = (List<Map<String,Object>>)JSON.parse(imagesDetail);
		//商品相册
//		HashMap<String,Object>[] imgArr = Json.fromJsonAsArray(NutMap.class, images);
		if (imgDetailArr != null) {
			for (int i = 0; i < imgDetailArr.size(); i++) {
				GoodsImagesDO goodsImage = new GoodsImagesDO();
				goodsImage.setGoodsId(goodsGoods.getId());
				goodsImage.setLocation(i);
				goodsImage.setAddress(1);
				goodsImage.setImgurl(imgDetailArr.get(i).get("url").toString());
				goodsImage.setId(UuidUtil.get32UUID());
				goodsImage.setGmtCreate(date);
				goodsImage.setUserIdCreate(getUserId());
				goodsImage.setDelFlag(Constant.DEFAULT_DEL_FLAG);
				goodsImagesService.save(goodsImage);
			}
		}
		/*ShopEstempDO estemp = new ShopEstempDO();
		estemp.setAction("create");
		estemp.setGoodsId(goodsGoods.getId());
		estemp.setId(UuidUtil.get32UUID());
		estemp.setOpBy(getUserId());
		estemp.setOpAt(date);
		estemp.setDelFlag(Constant.DEFAULT_DEL_FLAG);
		shopEstempService.save(estemp);*/
		return 1;
	
	}
	@Transactional(propagation= Propagation.REQUIRED)
	@Override
	public int edit(GoodsGoodsDO goodsGoods, String products, String spec_values, String param_values, String images,
			String imagesDetail) {
		if (spec_values.equals("[]") ) {
		} else {
			goodsGoods.setSpec(spec_values);
		}
		if (param_values.equals("[]") ) {
		} else {
			goodsGoods.setParam(param_values);
		}
		Date date = new Date();
		goodsGoods.setGmtModified(date);
		goodsGoods.setUserIdCreate(getUserId());
		goodsGoodsDao.update(goodsGoods);
		//删除商品相册信息
		goodsImagesService.removeByGoodsId(goodsGoods.getId());
		//删除商品货品信息
		goodsProductService.removeByGoodsIdJ(goodsGoods.getId());
//		goodsProductService.removeByGoodsId(goodsGoods.getId());

		//商品图片
		List<Map<String,Object>> imgArr = (List<Map<String,Object>>)JSON.parse(images);
		//商品相册
//		HashMap<String,Object>[] imgArr = Json.fromJsonAsArray(NutMap.class, images);
		if (imgArr != null) {
			String defaultImge = "";
			for (int i = 0; i < imgArr.size(); i++) {
				if (i == 0 || (boolean)imgArr.get(i).get("d")) defaultImge = imgArr.get(i).get("url").toString();
				GoodsImagesDO goodsImage = new GoodsImagesDO();
				goodsImage.setGoodsId(goodsGoods.getId());
				goodsImage.setLocation(i);
				goodsImage.setAddress(0);
				goodsImage.setImgurl(imgArr.get(i).get("url").toString());
				goodsImage.setId(UuidUtil.get32UUID());
				goodsImage.setGmtCreate(date);
				goodsImage.setUserIdCreate(getUserId());
				goodsImage.setDelFlag(Constant.DEFAULT_DEL_FLAG);
				goodsImagesService.save(goodsImage);
			}
			goodsGoods.setImgurl(defaultImge);
			goodsGoodsDao.update(goodsGoods);
		}

		//商品货品
		List<Map<String,Object>> productArr = (List<Map<String,Object>>) JSON.parse(products);
//		productArr
//		HashMap[] productArr = Json.fromJsonAsArray(HashMap.class, products);
		if (productArr != null) {
			if (goodsGoods.getHasSpec()==1 && StringUtils.isBlank(productArr.get(0).get("sku").toString())) {
				//如果没有启用规格，并且第一个货品SKU为null或空白字符则自动生成SKU
				productArr.get(0).put("sku", goodsProductService.getSkuPrefix());
			}
			for (int i = 0; i < productArr.size(); i++) {
				String productId = "";
				String name = "";
				String spec = productArr.get(i).get("spec").toString();
				GoodsProductDO product = new GoodsProductDO();
				if( productArr.get(i).containsKey("productId")){
					
						productId = productArr.get(i).get("productId").toString();
					
				}
				
				product.setGoodsId(goodsGoods.getId());
				product.setLocation(i);
				product.setSpec(spec);
				product.setUnit(goodsGoods.getUnit());
				product.setIcon(goodsGoods.getImgurl());
				//如果SKU为null或空白字符则自动生成SKU
				if (StringUtils.isBlank(productArr.get(i).get("sku").toString())) {
					product.setSku(goodsProductService.getSkuPrefix() + "-" + i);
				} else {
					product.setSku(productArr.get(i).get("sku").toString().toUpperCase());
					Map<String, Object> skuMap = new HashMap<>();
					skuMap.put("sku", product.getSku());
					List<GoodsProductDO> list = goodsProductService.list(skuMap);
					if(list.size()>0){
						productId=list.get(0).getId();
					}
				}
				//通过规格获取名称
				StringBuffer bufName = new StringBuffer();
				if (spec.contains("*") && spec.contains(":")) {
					String[] str = StringUtils.split(spec, "*");
					for (String temp : str) {
						String[] s = StringUtils.split(temp, ":");
//						name += s[1] + " ";
						bufName.append(s[1] + " ");
					}
				} else if (spec.contains(":")) {
					String[] s = StringUtils.split(spec, ":");
//					name = s[1];
					bufName.append(s[1]);
				}
				name=bufName.toString();
				product.setName(name);
				Boolean isDefault = (boolean)productArr.get(i).get("isDefault");
				if (isDefault){
					product.setIsDefault(1);
				}else {
					product.setIsDefault(0);
				}
				Boolean disabled = (boolean)productArr.get(i).get("disabled");
				if (disabled){
					product.setDisabled(1);
				}else {
					product.setDisabled(0);
					//上架时间
					product.setUpAt(date);
				}
				product.setStock(NumberUtils.toInt(productArr.get(i).get("stock").toString().replaceAll(" ", ""), 0));
				product.setBuyMin(NumberUtils.toInt(productArr.get(i).get("buyMin").toString().replaceAll(" ", ""), 1));
				product.setBuyMax(NumberUtils.toInt(productArr.get(i).get("buyMax").toString().replaceAll(" ", ""), 100));
				product.setWeight(NumberUtils.toInt(productArr.get(i).get("weight").toString().replaceAll(" ", ""), 0));
				product.setPrice(new BigDecimal(productArr.get(i).get("price").toString()));
				product.setPriceC(new BigDecimal(productArr.get(i).get("priceC").toString()));
				product.setPriceZ(new BigDecimal(productArr.get(i).get("priceZ").toString()));
				product.setPriceG(new BigDecimal(productArr.get(i).get("priceG").toString()));
				product.setPostFee(new BigDecimal(productArr.get(i).get("postFee").toString()));
				product.setRebateOne(new BigDecimal(productArr.get(i).get("rebateOne").toString()));
				product.setRebateTwo(new BigDecimal(productArr.get(i).get("rebateTwo").toString()));
				//设置默认参数
				product.setNumSale(0);
				product.setLocation(i);
				product.setUserIdCreate(getUserId());
				product.setGmtCreate(date);
				product.setDelFlag(Constant.DEFAULT_DEL_FLAG);
				if (StringUtils.isBlank(productId)){
					product.setId(UuidUtil.get32UUID());
					goodsProductService.save(product);
				}else {
					product.setId(productId);
					int update = goodsProductService.update(product);
					System.out.println(update);
				}
				
				//会员价
			}
		}

		//商品货品
		List<Map<String,Object>> imgDetailArr = (List<Map<String,Object>>)JSON.parse(imagesDetail);
		//商品相册
//		HashMap<String,Object>[] imgArr = Json.fromJsonAsArray(NutMap.class, images);
		if (imgDetailArr != null) {
			for (int i = 0; i < imgDetailArr.size(); i++) {
				GoodsImagesDO goodsImage = new GoodsImagesDO();
				goodsImage.setGoodsId(goodsGoods.getId());
				goodsImage.setLocation(i);
				goodsImage.setAddress(1);
				goodsImage.setImgurl(imgDetailArr.get(i).get("url").toString());
				goodsImage.setId(UuidUtil.get32UUID());
				goodsImage.setGmtCreate(date);
				goodsImage.setUserIdCreate(getUserId());
				goodsImage.setDelFlag(Constant.DEFAULT_DEL_FLAG);
				goodsImagesService.save(goodsImage);
			}
		}
		/*ShopEstempDO estemp = new ShopEstempDO();
		estemp.setAction("update");
		estemp.setGoodsId(goodsGoods.getId());
		estemp.setId(UUIDUtils.getUUID());
		estemp.setOpBy(getUserId());
		estemp.setOpAt(date);
		estemp.setDelFlag(Constant.DEFAULT_DEL_FLAG);
		shopEstempService.save(estemp);*/
		return 1;
	}

	/**
	 * 根据分类查询商品列表
	 */
	@Override
	public R goodsListByClass(Map<String, Object> params) {
		String userId = com.bootdo.common.utils.StringUtils.getString(params.get("userId"));
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		int level = 0;
		if (memberUserDO != null) {
			params.put("level", memberUserDO.getLevel());
			level = memberUserDO.getLevel();
		} else {
			params.put("level", 0);
			level = 0;
		}
		if ("".equals(params.get("order"))||StringUtils.isBlank(params.get("order").toString())){
			PageHelperUtils.page(params,"gg.up_at","desc");
		}else{
			PageHelperUtils.page(params,params.get("order").toString(),params.get("method").toString());
		}
		List<Map<String, Object>> goodsList =  goodsGoodsDao.goodsListByClass(params);
		for (Map<String, Object> goodsMap : goodsList) {
			int isvip = Integer.valueOf(goodsMap.get("isvip").toString());
			String imgurl = goodsMap.get("imgurl").toString();
			List<String> imgList = new ArrayList<String>();
			imgList.add(imgurl);
			goodsMap.remove("imgurl");
			goodsMap.put("imgList",imgList);
			Object price = goodsMap.get("price");
			Object price_c = goodsMap.get("price_c");
			Object price_z = goodsMap.get("price_z");
			Object price_g = goodsMap.get("price_g");
			if (isvip == 0) {
				goodsMap.put("minePrice",price);
			} else {
				switch (level) {
					case 1 :
						goodsMap.put("minePrice",price_c);
						break;
					case 2 :
						goodsMap.put("minePrice",price_z);
						break;
					case 3 :
						goodsMap.put("minePrice",price_g);
						break;
					default:
						goodsMap.put("minePrice",price);
				}
			}
		}

		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(goodsList);
		return R.ok().put("data", pageInfo);
	}
	
	
	/**
	 * 普通商品详情页
	 */
	@Override
	public R getDetail(Map<String, Object> params) {
		String goodsId = params.get("goodsId").toString();
		String userId = params.get("userId").toString();
		GoodsGoodsDO  goodsGoodsDO=goodsGoodsDao.getDetail(params);
		List<GoodsProductDO> goodsProductList = goodsProductDao.getGoodsByGoodsId(params);
		goodsGoodsDO.setGoodsProductList(goodsProductList);
		List<GoodsImagesDO> goodsImagesList = goodsImagesDao.getGoodsImgs(params);
		GoodsDto goodsDto = new GoodsDto();
		goodsDto.setGoodsId(goodsGoodsDO.getId());
		goodsDto.setName(goodsGoodsDO.getName());
		goodsDto.setNote(goodsGoodsDO.getNote());
		goodsDto.setNumSale(goodsGoodsDO.getNumSale());
		goodsDto.setParam(goodsGoodsDO.getParam());
		goodsDto.setSpec(goodsGoodsDO.getSpec());
		goodsDto.setTitle(goodsGoodsDO.getTitle());
		goodsDto.setImgurl(goodsGoodsDO.getImgurl());
		goodsDto.setCategory(0);

		goodsDto.setIsvip(goodsGoodsDO.getIsvip());
		goodsDto.setCopywriting(goodsGoodsDO.getCopywriting());
		goodsDto.setAllNumber(goodsGoodsDO.getAllNumber());
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		int level = memberUserDO.getLevel();
		int isvip = goodsGoodsDO.getIsvip();
		int postSize = goodsGoodsDO.getPostSize();  //运费模板类型  0：自定义运费，1：按重量模板

//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			if (goodsProductDO.getIsDefault()==1) {
				goodsDto.setStock(goodsProductDO.getStock());
				BigDecimal weight = new BigDecimal(goodsProductDO.getWeight()).divide(new BigDecimal(1000));
				BigDecimal postFee = goodsProductDO.getPostFee();
				if (postSize == 0) {
					goodsDto.setPostFee(postFee);
				} else {
					CmsContentDO contentDO = cmsContentDao.getByType("post-weight").get(0);
					BigDecimal value = new BigDecimal(contentDO.getValue());
					postFee = value.multiply(weight);
					goodsDto.setPostFee(postFee);
				}
				goodsDto.setPrice(goodsProductDO.getPrice());
				if (isvip == 0) {
					goodsDto.setMinePrice(goodsProductDO.getPrice());
				} else {
					switch (level) {
						case 1 :
							goodsDto.setMinePrice(goodsProductDO.getPriceC());
							break;
						case 2 :
							goodsDto.setMinePrice(goodsProductDO.getPriceZ());
							break;
						case 3 :
							goodsDto.setMinePrice(goodsProductDO.getPriceG());
							break;
						default:
							goodsDto.setMinePrice(goodsProductDO.getPrice());
					}
				}
			}
		}

		List<String> bannerList = new ArrayList<>();
		List<String> goodsImgList = new ArrayList<>();

		for (GoodsImagesDO goodsImagesDO : goodsImagesList) {
			if (goodsImagesDO.getAddress()==0) {
				bannerList.add(goodsImagesDO.getImgurl());
			}else if(goodsImagesDO.getAddress()==1){
				goodsImgList.add(goodsImagesDO.getImgurl());
			}
		}
		goodsDto.setBanner(bannerList);
		goodsDto.setGoodsImg(goodsImgList);

		//货品信息
		List<Map<String, Object>> productList = new ArrayList<>();
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			Map<String, Object> map = new HashMap<>();
			map.put("name", goodsProductDO.getName());
			map.put("icon", goodsProductDO.getIcon());
			map.put("spec", goodsProductDO.getSpec());
			map.put("postFee", goodsProductDO.getPostFee());
			map.put("stock", goodsProductDO.getStock());
			map.put("unit", goodsProductDO.getUnit());
			map.put("numNale", goodsProductDO.getNumSale());
			map.put("id", goodsProductDO.getId());
			if (isvip == 0) {
				map.put("price", goodsProductDO.getPrice());
			} else {
				switch (level) {
					case 1 :
						map.put("price", goodsProductDO.getPriceC());
						break;
					case 2 :
						map.put("price", goodsProductDO.getPriceZ());
						break;
					case 3 :
						map.put("price", goodsProductDO.getPriceG());
						break;
					default:
						map.put("price", goodsProductDO.getPrice());
				}
			}
			productList.add(map);
		}
		goodsDto.setProductList(productList);
		//是否关注
		Map<String,Object> searchMap = new HashMap<String,Object>();
		searchMap.put("goodsId",goodsId);
		searchMap.put("userId",userId);
		List list = memberFollowDao.list(searchMap);
		if (list.size() == 0) {
			goodsDto.setisFollow(false);
		} else {
			goodsDto.setisFollow(true);
		}
		//查询评论个数
		int comentSize = orderComentDao.getComentByGoods(params).size();
		goodsDto.setComentSize(comentSize);
		return R.ok().put("data", goodsDto);
	}

	/**
	 * 秒杀商品详情页
	 */
	@Override
	public R getSpikeDetail(Map<String, Object> params) {
		String spikeId = params.get("spikeId").toString();
		GoodsSpikeDO goodsSpikeDO = goodsSpikeService.get(spikeId);
		GoodsSpikeTimeDO goodsSpikeTimeDO = goodsSpikeTimeDao.get(goodsSpikeDO.getTimeId());
		String goodsId = goodsSpikeDO.getGoodsId();
		params.put("goodsId", goodsId);
		//GoodsSpikeTimeDO goodsSpikeTimeDO = goodsSpikeTimeDao.get(goodsSpikeDO.getTimeId());
		String userId = params.get("userId").toString();
		GoodsGoodsDO  goodsGoodsDO=goodsGoodsDao.getDetail(params);
		List<GoodsProductDO> goodsProductList = goodsProductDao.getGoodsByGoodsId(params);
		goodsGoodsDO.setGoodsProductList(goodsProductList);
		List<GoodsImagesDO> goodsImagesList = goodsImagesDao.getGoodsImgs(params);
		GoodsDto goodsDto = new GoodsDto();
		goodsDto.setId(spikeId);
		goodsDto.setGoodsId(goodsGoodsDO.getId());
		goodsDto.setName(goodsGoodsDO.getName());
		goodsDto.setNote(goodsGoodsDO.getNote());
		goodsDto.setNumSale(goodsSpikeDO.getNumSale());
		goodsDto.setParam(goodsGoodsDO.getParam());
		goodsDto.setSpec(goodsGoodsDO.getSpec());
		goodsDto.setTitle(goodsGoodsDO.getTitle());
		goodsDto.setImgurl(goodsGoodsDO.getImgurl());
		goodsDto.setCategory(1);
		SimpleDateFormat shiFormat = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat shisFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String today = shiFormat.format(new Date());
		try {
			goodsDto.setStartTime(shisFormat.parse(today+" "+goodsSpikeTimeDO.getStartAt().toString()));
			goodsDto.setEndTime(shisFormat.parse(today + " " + goodsSpikeTimeDO.getEndAt().toString()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		goodsDto.setIsvip(goodsGoodsDO.getIsvip());
		goodsDto.setCopywriting(goodsGoodsDO.getCopywriting());
		goodsDto.setAllNumber(goodsGoodsDO.getAllNumber());
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		int level = memberUserDO.getLevel();
		int isvip = goodsGoodsDO.getIsvip();
		int postSize = goodsGoodsDO.getPostSize();  //运费模板类型  0：自定义运费，1：按重量模板

//		MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			if (goodsProductDO.getIsDefault()==1) {
				goodsDto.setStock(goodsSpikeDO.getStock());
				goodsDto.setPrice(goodsSpikeDO.getPrice());

				BigDecimal weight = new BigDecimal(goodsProductDO.getWeight()).divide(new BigDecimal(100));
				BigDecimal postFee = goodsProductDO.getPostFee();
				if (postSize == 0) {
					goodsDto.setPostFee(postFee);
				} else {
					CmsContentDO contentDO = cmsContentDao.getByType("post-weight").get(0);
					BigDecimal value = new BigDecimal(contentDO.getValue());
					postFee = value.multiply(weight);
					goodsDto.setPostFee(postFee);
				}
				if (isvip == 0) {
					goodsDto.setMinePrice(goodsSpikeDO.getPrice());
				} else {
					switch (level) {
						case 1 :
							goodsDto.setMinePrice(goodsSpikeDO.getPriceC());
							break;
						case 2 :
							goodsDto.setMinePrice(goodsSpikeDO.getPriceZ());
							break;
						case 3 :
							goodsDto.setMinePrice(goodsSpikeDO.getPriceG());
							break;
						default:
							goodsDto.setMinePrice(goodsSpikeDO.getPrice());
					}
				}
			}
		}

		List<String> bannerList = new ArrayList<>();
		List<String> goodsImgList = new ArrayList<>();

		for (GoodsImagesDO goodsImagesDO : goodsImagesList) {
			if (goodsImagesDO.getAddress()==0) {
				bannerList.add(goodsImagesDO.getImgurl());
			}else if(goodsImagesDO.getAddress()==1){
				goodsImgList.add(goodsImagesDO.getImgurl());
			}
		}
		goodsDto.setBanner(bannerList);
		goodsDto.setGoodsImg(goodsImgList);

		//货品信息
		List<Map<String, Object>> productList = new ArrayList<>();
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			Map<String, Object> map = new HashMap<>();
			map.put("name", goodsProductDO.getName());
			map.put("icon", goodsProductDO.getIcon());
			map.put("spec", goodsProductDO.getSpec());
			//map.put("price_market", goodsProductDO.getPriceMarket());
			map.put("stock", goodsProductDO.getStock());
			map.put("unit", goodsProductDO.getUnit());
			map.put("numNale", goodsProductDO.getNumSale());
			map.put("id", goodsProductDO.getId());
			if (isvip == 0) {
				map.put("price", goodsSpikeDO.getPrice());
			} else {
				switch (level) {
					case 1 :
						map.put("price", goodsSpikeDO.getPriceC());
						break;
					case 2 :
						map.put("price", goodsSpikeDO.getPriceZ());
						break;
					case 3 :
						map.put("price", goodsSpikeDO.getPriceG());
						break;
					default:
						map.put("price", goodsSpikeDO.getPrice());
				}
			}
			productList.add(map);
		}
		goodsDto.setProductList(productList);
		//是否关注
		Map<String,Object> searchMap = new HashMap<String,Object>();
		searchMap.put("goodsId",goodsId);
		searchMap.put("userId",userId);
		List list = memberFollowDao.list(searchMap);
		if (list.size() == 0) {
			goodsDto.setisFollow(false);
		} else {
			goodsDto.setisFollow(true);
		}
		//查询评论个数
		int comentSize = orderComentDao.getComentByGoods(params).size();
		goodsDto.setComentSize(comentSize);
		return R.ok().put("data", goodsDto);
	}

	/**
	 * 会员专区商品详情页
	 */
	@Override
	public R getUpgradeDetail(Map<String, Object> params) {
		String upgradeId = params.get("upgradeId").toString();
		String userId = params.get("userId").toString();
		GoodsUpgradeDO goodsUpgradeDO = goodsUpgradeDao.get(upgradeId);
		String goodsId = goodsUpgradeDO.getGoodsId();
		params.put("goodsId", goodsId);
		GoodsGoodsDO  goodsGoodsDO=goodsGoodsDao.getDetail(params);
		List<GoodsProductDO> goodsProductList = goodsProductDao.getGoodsByGoodsId(params);
		goodsGoodsDO.setGoodsProductList(goodsProductList);
		List<GoodsImagesDO> goodsImagesList = goodsImagesDao.getGoodsImgs(params);
		GoodsDto goodsDto = new GoodsDto();
		goodsDto.setId(upgradeId);
		goodsDto.setGoodsId(goodsGoodsDO.getId());
		goodsDto.setName(goodsGoodsDO.getName());
		goodsDto.setNote(goodsGoodsDO.getNote());
		goodsDto.setNumSale(goodsUpgradeDO.getNumSale());
		goodsDto.setParam(goodsGoodsDO.getParam());
		goodsDto.setSpec(goodsGoodsDO.getSpec());
		goodsDto.setTitle(goodsGoodsDO.getTitle());
		goodsDto.setImgurl(goodsGoodsDO.getImgurl());
		goodsDto.setCategory(5);

		goodsDto.setIsvip(goodsGoodsDO.getIsvip());
		goodsDto.setCopywriting(goodsGoodsDO.getCopywriting());
		goodsDto.setAllNumber(goodsGoodsDO.getAllNumber());
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		int postSize = goodsGoodsDO.getPostSize();  //运费模板类型  0：自定义运费，1：按重量模板

//		MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			if (goodsProductDO.getIsDefault()==1) {
				goodsDto.setStock(goodsUpgradeDO.getStock());
				goodsDto.setPrice(goodsProductDO.getPrice());
				goodsDto.setMinePrice(goodsUpgradeDO.getPrice());
				BigDecimal weight = new BigDecimal(goodsProductDO.getWeight()).divide(new BigDecimal(100));
				BigDecimal postFee = goodsProductDO.getPostFee();
				if (postSize == 0) {
					goodsDto.setPostFee(postFee);
				} else {
					CmsContentDO contentDO = cmsContentDao.getByType("post-weight").get(0);
					BigDecimal value = new BigDecimal(contentDO.getValue());
					postFee = value.multiply(weight);
					goodsDto.setPostFee(postFee);
				}
			}
		}

		List<String> bannerList = new ArrayList<>();
		List<String> goodsImgList = new ArrayList<>();

		for (GoodsImagesDO goodsImagesDO : goodsImagesList) {
			if (goodsImagesDO.getAddress()==0) {
				bannerList.add(goodsImagesDO.getImgurl());
			}else if(goodsImagesDO.getAddress()==1){
				goodsImgList.add(goodsImagesDO.getImgurl());
			}
		}
		goodsDto.setBanner(bannerList);
		goodsDto.setGoodsImg(goodsImgList);

		//货品信息
		List<Map<String, Object>> productList = new ArrayList<>();
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			Map<String, Object> map = new HashMap<>();
			map.put("name", goodsProductDO.getName());
			map.put("icon", goodsProductDO.getIcon());
			map.put("spec", goodsProductDO.getSpec());
			//map.put("price_market", goodsProductDO.getPriceMarket());
			map.put("stock", goodsProductDO.getStock());
			map.put("unit", goodsProductDO.getUnit());
			map.put("numNale", goodsProductDO.getNumSale());
			map.put("id", goodsProductDO.getId());
			map.put("price", goodsUpgradeDO.getPrice());
			productList.add(map);
		}
		goodsDto.setProductList(productList);
		//是否关注
		Map<String,Object> searchMap = new HashMap<String,Object>();
		searchMap.put("goodsId",goodsId);
		searchMap.put("userId",userId);
		List list = memberFollowDao.list(searchMap);
		if (list.size() == 0) {
			goodsDto.setisFollow(false);
		} else {
			goodsDto.setisFollow(true);
		}
		//查询评论个数
		int comentSize = orderComentDao.getComentByGoods(params).size();
		goodsDto.setComentSize(comentSize);
		return R.ok().put("data", goodsDto);
	}

	/**
	 * 拼团专区商品详情页
	 */
	@Override
	public R getTeamDetail(Map<String, Object> params) {
		String teamId = params.get("teamId").toString();
		String userId = params.get("userId").toString();
		GoodsTeamDO goodsTeamDO = goodsTeamDao.get(teamId);
		String goodsId = goodsTeamDO.getGoodsId();
		params.put("goodsId", goodsId);
		GoodsGoodsDO  goodsGoodsDO=goodsGoodsDao.getDetail(params);
		List<GoodsProductDO> goodsProductList = goodsProductDao.getGoodsByGoodsId(params);
		goodsGoodsDO.setGoodsProductList(goodsProductList);
		List<GoodsImagesDO> goodsImagesList = goodsImagesDao.getGoodsImgs(params);
		GoodsDto goodsDto = new GoodsDto();
		goodsDto.setId(teamId);
		goodsDto.setGoodsId(goodsGoodsDO.getId());
		goodsDto.setName(goodsGoodsDO.getName());
		goodsDto.setNote(goodsGoodsDO.getNote());
		goodsDto.setNumSale(goodsTeamDO.getNumSale());
		goodsDto.setParam(goodsGoodsDO.getParam());
		goodsDto.setSpec(goodsGoodsDO.getSpec());
		goodsDto.setTitle(goodsGoodsDO.getTitle());
		goodsDto.setImgurl(goodsGoodsDO.getImgurl());
		goodsDto.setCategory(4);

		goodsDto.setIsvip(goodsGoodsDO.getIsvip());
		goodsDto.setCopywriting(goodsGoodsDO.getCopywriting());
		goodsDto.setAllNumber(goodsGoodsDO.getAllNumber());
		goodsDto.setNumber(goodsTeamDO.getNumber());
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		int level = memberUserDO.getLevel();
		int isvip = goodsGoodsDO.getIsvip();
		int postSize = goodsGoodsDO.getPostSize();  //运费模板类型  0：自定义运费，1：按重量模板

//		MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			if (goodsProductDO.getIsDefault()==1) {
				goodsDto.setStock(goodsTeamDO.getStock());
				goodsDto.setPrice(goodsTeamDO.getPrice());
				BigDecimal weight = new BigDecimal(goodsProductDO.getWeight()).divide(new BigDecimal(100));
				BigDecimal postFee = goodsProductDO.getPostFee();
				if (postSize == 0) {
					goodsDto.setPostFee(postFee);
				} else {
					CmsContentDO contentDO = cmsContentDao.getByType("post-weight").get(0);
					BigDecimal value = new BigDecimal(contentDO.getValue());
					postFee = value.multiply(weight);
					goodsDto.setPostFee(postFee);
				}
				if (isvip == 0) {
					goodsDto.setMinePrice(goodsTeamDO.getPrice());
				} else {
					switch (level) {
						case 1 :
							goodsDto.setMinePrice(goodsTeamDO.getPriceC());
							break;
						case 2 :
							goodsDto.setMinePrice(goodsTeamDO.getPriceZ());
							break;
						case 3 :
							goodsDto.setMinePrice(goodsTeamDO.getPriceG());
							break;
						default:
							goodsDto.setMinePrice(goodsTeamDO.getPrice());
					}
				}
			}
		}

		List<String> bannerList = new ArrayList<>();
		List<String> goodsImgList = new ArrayList<>();

		for (GoodsImagesDO goodsImagesDO : goodsImagesList) {
			if (goodsImagesDO.getAddress()==0) {
				bannerList.add(goodsImagesDO.getImgurl());
			}else if(goodsImagesDO.getAddress()==1){
				goodsImgList.add(goodsImagesDO.getImgurl());
			}
		}
		goodsDto.setBanner(bannerList);
		goodsDto.setGoodsImg(goodsImgList);

		//货品信息
		List<Map<String, Object>> productList = new ArrayList<>();
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			Map<String, Object> map = new HashMap<>();
			map.put("name", goodsProductDO.getName());
			map.put("icon", goodsProductDO.getIcon());
			map.put("spec", goodsProductDO.getSpec());
			//map.put("price_market", goodsProductDO.getPriceMarket());
			map.put("stock", goodsProductDO.getStock());
			map.put("unit", goodsProductDO.getUnit());
			map.put("numNale", goodsProductDO.getNumSale());
			map.put("id", goodsProductDO.getId());
			if (isvip == 0) {
				map.put("price", goodsTeamDO.getPrice());
			} else {
				switch (level) {
					case 1 :
						map.put("price", goodsTeamDO.getPriceC());
						break;
					case 2 :
						map.put("price", goodsTeamDO.getPriceZ());
						break;
					case 3 :
						map.put("price", goodsTeamDO.getPriceG());
						break;
					default:
						map.put("price", goodsTeamDO.getPrice());
				}
			}
			productList.add(map);
		}
		goodsDto.setProductList(productList);
		//是否关注
		Map<String,Object> searchMap = new HashMap<String,Object>();
		searchMap.put("goodsId",goodsId);
		searchMap.put("userId",userId);
		List list = memberFollowDao.list(searchMap);
		if (list.size() == 0) {
			goodsDto.setisFollow(false);
		} else {
			goodsDto.setisFollow(true);
		}
		//查询评论个数
		int comentSize = orderComentDao.getComentByGoods(params).size();
		goodsDto.setComentSize(comentSize);
		return R.ok().put("data", goodsDto);
	}


	/**
	 * 9.9专区商品详情页
	 */
	@Override
	public R getNineDetail(Map<String, Object> params) {
		String nineId = params.get("nineId").toString();
		String userId = params.get("userId").toString();
		GoodsNineDO goodsNineDO = goodsNineDao.get(nineId);
		String goodsId = goodsNineDO.getGoodsId();
		params.put("goodsId", goodsId);
		GoodsGoodsDO  goodsGoodsDO=goodsGoodsDao.getDetail(params);
		List<GoodsProductDO> goodsProductList = goodsProductDao.getGoodsByGoodsId(params);
		goodsGoodsDO.setGoodsProductList(goodsProductList);
		List<GoodsImagesDO> goodsImagesList = goodsImagesDao.getGoodsImgs(params);
		GoodsDto goodsDto = new GoodsDto();
		goodsDto.setId(nineId);
		goodsDto.setGoodsId(goodsGoodsDO.getId());
		goodsDto.setName(goodsGoodsDO.getName());
		goodsDto.setNote(goodsGoodsDO.getNote());
		goodsDto.setNumSale(goodsNineDO.getNumSale());
		goodsDto.setParam(goodsGoodsDO.getParam());
		goodsDto.setSpec(goodsGoodsDO.getSpec());
		goodsDto.setTitle(goodsGoodsDO.getTitle());
		goodsDto.setImgurl(goodsGoodsDO.getImgurl());
		goodsDto.setCategory(2);

//		MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			if (goodsProductDO.getIsDefault()==1) {
				goodsDto.setStock(goodsNineDO.getStock());
				goodsDto.setPrice(goodsProductDO.getPrice());
				goodsDto.setPostFee(goodsProductDO.getPostFee());
				goodsDto.setMinePrice(goodsNineDO.getPrice());
			}
		}

		List<String> bannerList = new ArrayList<>();
		List<String> goodsImgList = new ArrayList<>();

		for (GoodsImagesDO goodsImagesDO : goodsImagesList) {
			if (goodsImagesDO.getAddress()==0) {
				bannerList.add(goodsImagesDO.getImgurl());
			}else if(goodsImagesDO.getAddress()==1){
				goodsImgList.add(goodsImagesDO.getImgurl());
			}
		}
		goodsDto.setBanner(bannerList);
		goodsDto.setGoodsImg(goodsImgList);

		//货品信息
		List<Map<String, Object>> productList = new ArrayList<>();
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			Map<String, Object> map = new HashMap<>();
			map.put("name", goodsProductDO.getName());
			map.put("icon", goodsProductDO.getIcon());
			map.put("spec", goodsProductDO.getSpec());
			//map.put("price_market", goodsProductDO.getPriceMarket());
			map.put("stock", goodsProductDO.getStock());
			map.put("unit", goodsProductDO.getUnit());
			map.put("numNale", goodsProductDO.getNumSale());
			map.put("id", goodsProductDO.getId());
			map.put("price", goodsNineDO.getPrice());
			productList.add(map);
		}
		goodsDto.setProductList(productList);
		//是否关注
		Map<String,Object> searchMap = new HashMap<String,Object>();
		searchMap.put("goodsId",goodsId);
		searchMap.put("userId",userId);
		List list = memberFollowDao.list(searchMap);
		if (list.size() == 0) {
			goodsDto.setisFollow(false);
		} else {
			goodsDto.setisFollow(true);
		}
		//查询评论个数
		int comentSize = orderComentDao.getComentByGoods(params).size();
		goodsDto.setComentSize(comentSize);
		return R.ok().put("data", goodsDto);
	}

	/**
	 * 砍价商品详情页
	 */
	@Override
	public R getBargainDetail(Map<String, Object> params) {
		String bargainId = params.get("bargainId").toString();
		String userId = params.get("userId").toString();
		GoodsBargainDO goodsBargainDO = goodsBargainDao.get(bargainId);
		String goodsId = goodsBargainDO.getGoodsId();
		params.put("goodsId", goodsId);
		GoodsGoodsDO  goodsGoodsDO=goodsGoodsDao.getDetail(params);
		List<GoodsProductDO> goodsProductList = goodsProductDao.getGoodsByGoodsId(params);
		goodsGoodsDO.setGoodsProductList(goodsProductList);
		List<GoodsImagesDO> goodsImagesList = goodsImagesDao.getGoodsImgs(params);
		GoodsDto goodsDto = new GoodsDto();
		goodsDto.setId(bargainId);
		goodsDto.setGoodsId(goodsGoodsDO.getId());
		goodsDto.setName(goodsGoodsDO.getName());
		goodsDto.setNote(goodsGoodsDO.getNote());
		goodsDto.setNumSale(goodsBargainDO.getNumSale());
		goodsDto.setParam(goodsGoodsDO.getParam());
		goodsDto.setSpec(goodsGoodsDO.getSpec());
		goodsDto.setTitle(goodsGoodsDO.getTitle());
		goodsDto.setImgurl(goodsGoodsDO.getImgurl());
		goodsDto.setCategory(3);

//		MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			if (goodsProductDO.getIsDefault()==1) {
				goodsDto.setStock(goodsBargainDO.getStock());
				goodsDto.setPrice(goodsProductDO.getPrice());
				goodsDto.setPostFee(goodsProductDO.getPostFee());
				goodsDto.setMinePrice(goodsBargainDO.getPrice());
			}
		}

		List<String> bannerList = new ArrayList<>();
		List<String> goodsImgList = new ArrayList<>();

		for (GoodsImagesDO goodsImagesDO : goodsImagesList) {
			if (goodsImagesDO.getAddress()==0) {
				bannerList.add(goodsImagesDO.getImgurl());
			}else if(goodsImagesDO.getAddress()==1){
				goodsImgList.add(goodsImagesDO.getImgurl());
			}
		}
		goodsDto.setBanner(bannerList);
		goodsDto.setGoodsImg(goodsImgList);

		//货品信息
		List<Map<String, Object>> productList = new ArrayList<>();
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			Map<String, Object> map = new HashMap<>();
			map.put("name", goodsProductDO.getName());
			map.put("icon", goodsProductDO.getIcon());
			map.put("spec", goodsProductDO.getSpec());
			//map.put("price_market", goodsProductDO.getPriceMarket());
			map.put("stock", goodsProductDO.getStock());
			map.put("unit", goodsProductDO.getUnit());
			map.put("numNale", goodsProductDO.getNumSale());
			map.put("id", goodsProductDO.getId());
			map.put("price", goodsBargainDO.getPrice());
			productList.add(map);
		}
		goodsDto.setProductList(productList);
		//是否关注
		Map<String,Object> searchMap = new HashMap<String,Object>();
		searchMap.put("goodsId",goodsId);
		searchMap.put("userId",userId);
		List list = memberFollowDao.list(searchMap);
		if (list.size() == 0) {
			goodsDto.setisFollow(false);
		} else {
			goodsDto.setisFollow(true);
		}
		//查询评论个数
		int comentSize = orderComentDao.getComentByGoods(params).size();
		goodsDto.setComentSize(comentSize);
		return R.ok().put("data", goodsDto);
	}



	private void removePrice(Map<String, Object> map) {
		map.remove("price_f");
		map.remove("price_l");
		map.remove("price_h");

	}

	/*@Override
	public R getGroupGoods(Map<String, Object> params) {
		PageHelperUtils.page(params);
		List<Map<String, Object>> goodsList = goodsGoodsDao.getGroupGoods(params);
		MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
		for (Map<String, Object> map : goodsList) {
		
			if(memberUserDO.getLevel()==0){
				map.put("price", map.get("price_f"));
			}else if (memberUserDO.getLevel()==1){
				map.put("price", map.get("price_h"));
			}else if (memberUserDO.getLevel()==2){
				map.put("price", map.get("price_l"));
			}
			removePrice(map);
		}
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(goodsList);
		
		return R.ok().put("data", pageInfo);
	}*/

	/*@Override
	public R getSpike(Map<String, Object> params) {
		PageHelperUtils.page(params);
		List<Map<String, Object>> goodsList  = goodsGoodsDao.getSpike();
		MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
		for (Map<String, Object> map : goodsList) {
		
			if(memberUserDO.getLevel()==0){
				map.put("price", map.get("price_f"));
			}else if (memberUserDO.getLevel()==1){
				map.put("price", map.get("price_h"));
			}else if (memberUserDO.getLevel()==2){
				map.put("price", map.get("price_l"));
			}
			removePrice(map);
		}
		
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(goodsList);
		return R.ok().put("data", pageInfo);
	}*/

	/**
	 * 热卖、推荐商品列表
	 */
	@Override
	public R recommend(Map<String, Object> params) {
		String userId = com.bootdo.common.utils.StringUtils.getString(params.get("userId"));
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		int level = 0;
		if (memberUserDO != null) {
			params.put("level", memberUserDO.getLevel());
			level = memberUserDO.getLevel();
		} else {
			params.put("level", 0);
			level = 0;
		}
		PageHelperUtils.page(params);
		List<Map<String, Object>> goodsList = goodsGoodsDao.recommend(params);
		for (Map<String, Object> goodsMap : goodsList) {
			int isvip = Integer.valueOf(goodsMap.get("isvip").toString());
			String imgurl = goodsMap.get("imgurl").toString();
			List<String> imgList = new ArrayList<String>();
			imgList.add(imgurl);
			goodsMap.remove("imgurl");
			goodsMap.put("imgList",imgList);
			Object price = goodsMap.get("price");
			Object price_c = goodsMap.get("price_c");
			Object price_z = goodsMap.get("price_z");
			Object price_g = goodsMap.get("price_g");
			if (isvip == 0) {
				goodsMap.put("minePrice",price);
			} else {
				switch (level) {
					case 1 :
						goodsMap.put("minePrice",price_c);
						break;
					case 2 :
						goodsMap.put("minePrice",price_z);
						break;
					case 3 :
						goodsMap.put("minePrice",price_g);
						break;
					default:
						goodsMap.put("minePrice",price);
				}
			}
		}
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(goodsList);
		return R.ok().put("data", pageInfo);
	}

	@Override
	public Map<String,Object> getCmsGoods(Map<String, Object> params) {
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String,Object>> nineList =goodsNineDao.getNineGoodsList(params);
		List<Map<String,Object>> apikeList =goodsSpikeDao.getGoodsByTime(params);
        List<Map<String,Object>> hotList = goodsGoodsDao.getHotList(params);
        map.put("nineList",nineList);
        map.put("spikeList", apikeList);
        map.put("hotList",hotList);
        return map;
	}

	@Override
	public R isCanBye(Map<String,Object> params) {
		String productId = params.get("productId").toString();
		String userId = params.get("userId").toString();
		String xId = params.get("xId").toString();    // goodsId\ nineId\ spikeId
		int num = Integer.valueOf(params.get("num").toString());
		int type = Integer.valueOf(params.get("type").toString());  //0：普通商品，1:9.9商品，2：秒杀商品，4：拼团商品，5：会员专区商品
		GoodsProductDO product = goodsProductService.get(productId);
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		GoodsGoodsDO goodsDO = goodsGoodsDao.get(product.getGoodsId());
		int p_disabled = product.getDisabled();
		int p_del = product.getDelFlag();
		int g_del = goodsDO.getDelFlag();
		int g_disabled = goodsDO.getDisabled();
		int level = memberUserDO.getLevel();  //个人等级
		String vipLevel = goodsDO.getVipLevel();  //商品允许等级
		int allNumber = goodsDO.getAllNumber();   //商品限购量

		if (p_del == 1 || p_disabled == 1 || g_del == 1 || g_disabled == 1) {
			return R.error(2,"该商品已下架");
		}
		if (num == 0) {
			return R.error(1,"商品数量不能为0");
		}
		int min = product.getBuyMin();
		int max = product.getBuyMax();
		if (num < min) {
			return R.error(1,"该商品最小购买量为"+min);
		} else if (num > max) {
			return R.error(1,"该商品最大购买量为"+max);
		}
		//判断是否有资格购买
		if (type != 4 && vipLevel.indexOf(level+"") == -1) {
			return R.error(3,"该商品您没有权限购买");
		}
		//判断商品限购数量
		Map map = new HashMap<String,Object>();
		map.put("userId",userId);
		map.put("goodsId",goodsDO.getId());
		int canBye = allNumber - goodsGoodsDao.getSumByUser(map);
		if (num > canBye) {
			return R.error(4,"此商品已达到限购数量");
		}
		//判断库存
		int stock = product.getStock();  //库存
		if (stock < num) {
			return R.error(5,"商品库存不足");
		}
		if (type == 2) {     //秒杀商品
			GoodsSpikeDO spikeDO = goodsSpikeDao.get(xId);
			int spikeNum = spikeDO.getStock();
			if (spikeNum < num) {
				return R.error("该秒杀商品库存不足");
			}
			//判断秒杀时间
			String timeId = spikeDO.getTimeId();
			GoodsSpikeTimeDO goodsSpikeTimeDO = goodsSpikeTimeDao.get(timeId);
			SimpleDateFormat shiFormat = new SimpleDateFormat("HH:mm");
			Date startAt = null;
			Date endAt = null;
			Date now = null;
			try {
				now = shiFormat.parse(shiFormat.format(new Date()).toString());
				startAt = shiFormat.parse(goodsSpikeTimeDO.getStartAt().toString());
				endAt = shiFormat.parse(goodsSpikeTimeDO.getEndAt().toString());
			} catch (ParseException e) {
				e.printStackTrace();
			}
			if (startAt.after(now)) {
				return R.error("该商品秒杀还没开始");
			}
			if (!endAt.after(now)) {
				return R.error("该商品秒杀已结束");
			}
		} else if (type == 1) {   //9.9商品
			GoodsNineDO nineDO = goodsNineDao.get(xId);
			int nineNum = nineDO.getStock();
			if (nineNum < num) {
				return R.error("该专区商品库存不足");
			}
		} else if (type == 4) {   //拼团商品
			GoodsTeamDO teamDO = goodsTeamDao.get(xId);
			int teamNum = teamDO.getStock();
			if (teamNum < num) {
				return R.error("该专区商品库存不足");
			}
			if (num != 1) {
				return R.error("该专区商品一次只能买一件");
			}
		} else if (type == 5) {   //会员商品
			GoodsUpgradeDO upgradeDO = goodsUpgradeDao.get(xId);
			int upgradeNum = upgradeDO.getStock();
			if (upgradeNum < num) {
				return R.error("该专区商品库存不足");
			}
			int goodsLevel = upgradeDO.getLevel();
			if (!(goodsLevel>level)) {
				return R.error("您已经升级过该会员了");
			}
			if (num != 1) {
				return R.error("该专区商品一次只能买一件");
			}
		}
		return R.ok();
	}

	/*获取后台商品列表*/
	public List<Map<String, Object>> getList(Map<String, Object> map) {
		return goodsGoodsDao.getList(map);
	}

	
}
